<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
            <el-form-item label="姓名" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="合同类型" prop="htType">
                <el-select v-model="queryParams.htType" style="width: 200px;" clearable placeholder="请选择操合同类型">
                    <el-option
                        v-for="item in swList"
                        :key="item.value"
                        :label="item.label"
                        :value="Number(item.value)"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="合同状态" prop="htStatus">
                <el-select v-model="queryParams.htStatus" style="width: 200px;" clearable placeholder="请选择操合同状态">
                    <el-option
                        v-for="item in swList"
                        :key="item.value"
                        :label="item.label"
                        :value="Number(item.value)"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="签署状态" prop="userName">
                <el-select v-model="queryParams.userName" style="width: 200px;" clearable placeholder="请选择签署状态">
                    <el-option
                        v-for="item in swList"
                        :key="item.value"
                        :label="item.label"
                        :value="Number(item.value)"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="签订时间" prop="qdDateRange">
                <el-date-picker
                v-model="queryParams.qdDateRange"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetimerange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期" 
                :default-time="['00:00:00','23:59:59']"
                ></el-date-picker>
            </el-form-item>
            <el-form-item label="起始时间" prop="qsDateRange">
                <el-date-picker
                v-model="queryParams.qsDateRange"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetimerange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期" 
                :default-time="['00:00:00','23:59:59']"
                ></el-date-picker>
            </el-form-item>
            <el-form-item label="截止时间" prop="jzDateRange">
                <el-date-picker
                v-model="queryParams.jzDateRange"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetimerange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期" 
                :default-time="['00:00:00','23:59:59']"
                ></el-date-picker>
            </el-form-item>
            <el-form-item label="合同发送时间" prop="htfsDateRange" label-width="108px">
                <el-date-picker
                v-model="queryParams.htfsDateRange"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetimerange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期" 
                :default-time="['00:00:00','23:59:59']"
                ></el-date-picker>
            </el-form-item>
            <el-form-item label="合同签约方式" prop="htqyfs" label-width="108px">
                <el-select v-model="queryParams.htqyfs" style="width: 200px;" clearable placeholder="请选择合同签约方式">
                    <el-option
                        v-for="item in swList"
                        :key="item.value"
                        :label="item.label"
                        :value="Number(item.value)"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="关联项目" prop="glProject">
                <el-select v-model="queryParams.glProject" style="width: 200px;" clearable placeholder="请选择关联项目">
                    <el-option
                        v-for="item in swList"
                        :key="item.value"
                        :label="item.label"
                        :value="Number(item.value)"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="公司/大区/业务单元" prop="gsyewudanyuan" label-width="148px">
                <el-select v-model="queryParams.gsyewudanyuan" style="width: 200px;" clearable placeholder="请选择公司/大区/业务单元">
                    <el-option
                        v-for="item in swList"
                        :key="item.value"
                        :label="item.label"
                        :value="Number(item.value)"
                    ></el-option>
                </el-select>
            </el-form-item>
            
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <TableCard>
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" >
                        新增
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="info" plain icon="el-icon-upload2" size="mini" >导入</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" >
                        导出
                    </el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>

            <el-table :border="Global.tableShowBorder" v-loading="loading" :data="communicateBookList">
                <el-table-column v-for="(item,index) in tableList" :key="index" :label="item.label" align="center" :prop="item.prop">
                    <template slot-scope="{row}">
                        {{row[item.prop]}}
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="240px">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" icon="el-icon-view" @click="handleUpdate(scope.row)" >
                            查看
                        </el-button>
                        <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" >
                            修改
                        </el-button>
                        <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>
        </TableCard>

        <!-- 添加或修改个人通讯录对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-row>
                    <el-col :span="12" v-for="(item,index) in tableList" :key="index">
                        <el-form-item :label="item.label" :prop="item.prop">
                            <el-input v-model="form[item.prop]" :placeholder="`请输入${item.label}`"/>
                        </el-form-item>
                    </el-col>
                </el-row>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listCommunicateBook,
    getCommunicateBook,
    delCommunicateBook,
    addCommunicateBook,
    updateCommunicateBook
} from "@/api/common/communicateBook";

export default {
    name: "custoManag",
    data() {
        return {
            swList:[
                {label:'张祁阳',value:1,},
                {label:'王少奇',value:2,},{label:'黄杜娟',value:3,},
            ],
            tableList:[
                {label:'签署状态',prop:'qsStatus',},{label:'姓名',prop:'name',},{label:'合同签约方式',prop:'qyType',},
                {label:'是否执行',prop:'isZhixing',},{label:'签约主体',prop:'qianyuezhuti',},
                {label:'项目',prop:'project',},{label:'公司',prop:'gongsi',},{label:'大区',prop:'daqu',},
            ],
            baseUrl: process.env.VUE_APP_BASE_API,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 个人通讯录表格数据
            communicateBookList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                orderByColumn: "createTime",
                isAsc: "descending",
                userId: null,
                avatar: null,
                name: null,
                birthDate: null,
                idNum: null,
                phone: null,
                email: null,
                companyName: null,
                companyAddress: null,
                address: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                name: [
                    {required: true, message: "姓名不能为空", trigger: "blur"}
                ],
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询个人通讯录列表 */
        getList() {
            this.communicateBookList = [
                {
                    qsStatus:'待处理',gongsi:'厦门万云科技有限公司',name:'张祁阳',qyType:'线下',isZhixing:'不执行',
                    qianyuezhuti:'厦门万云科技有限公司',project:'',daqu:'',
                },
                {
                    qsStatus:'待处理',gongsi:'厦门万云科技有限公司',name:'黄杜娟',qyType:'线下',isZhixing:'不执行',
                    qianyuezhuti:'厦门万云科技有限公司',project:'',daqu:'',
                },
                {
                    qsStatus:'待处理',gongsi:'厦门万云科技有限公司',name:'张祁阳',qyType:'线下',isZhixing:'不执行',
                    qianyuezhuti:'厦门万云科技有限公司',project:'',daqu:'',
                },
                {
                    qsStatus:'待处理',gongsi:'厦门万云科技有限公司',name:'黄杜娟',qyType:'线下',isZhixing:'不执行',
                    qianyuezhuti:'厦门万云科技有限公司',project:'',daqu:'',
                },
                {
                    qsStatus:'待处理',gongsi:'厦门万云科技有限公司',name:'王少奇',qyType:'线下',isZhixing:'不执行',
                    qianyuezhuti:'厦门万云科技有限公司',project:'',daqu:'',
                },
            ]
            this.loading = false
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                userId: null,
                avatar: null,
                name: null,
                birthDate: null,
                idNum: null,
                phone: null,
                email: null,
                remark: null,
                companyName: null,
                companyAddress: null,
                updateTime: null,
                createTime: null,
                address: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加客户单位管理";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            this.form = {...row}
            this.open = true;
            this.title = "修改客户单位管理";
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateCommunicateBook(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addCommunicateBook(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除该数据项？').then(function () {
                return delCommunicateBook(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('oa/communicateBook/export', {
                ...this.queryParams
            }, `communicateBook_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
